package dao;

import com.alibaba.druid.util.StringUtils;
import modle.Admin;
import modle.vo.AdminInfoVO;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import util.DruidUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ZeYu
 * @Date: 2022/3/13 16:11
 */
public class AdminDaoImpl implements AdminDao {
    @Override
    public int login(Admin admin) {
        QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());
        try {
            Long count = queryRunner.query("select count(*) from admin where username=? and password=?", new ScalarHandler<>(), admin.getUsername(), admin.getPassword());
            if(count.intValue() == 1){
                return 200;
            }else {
                return 404;
            }




        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 500;
    }



    @Override
    public List<AdminInfoVO> allAdmins(Admin admin) {
        //先组装sql和参数，最后再执行
        Map<String,Object> result= getDynamicSql(admin);
        String sql= (String) result.get("sql");
        List<String> params = (List<String>) result.get("params");


        QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());
        List<AdminInfoVO> adminInfoVOS = null;
        try {
//          adminInfoVOS = queryRunner.query("select id,username as email,nickname,password as pwd from admin", new BeanListHandler<AdminInfoVO>(AdminInfoVO.class));
            adminInfoVOS = queryRunner.query(sql, new BeanListHandler<AdminInfoVO>(AdminInfoVO.class),params.toArray());



        } catch (SQLException e) {
            e.printStackTrace();
        }
        return adminInfoVOS;

    }

    private Map<String, Object> getDynamicSql(Admin admin) {
//        String baseSql="select id,username as email,nickname ,password as pwd  from admin where 1 = 1";
        String baseSql="select id, username as email , nickname ,password as pwd from admin where 1=1";
//        String baseSql="select id,username as email,nickname,password as pwd from admin where 1 = 1 ";

        List<String> params=new ArrayList<>();
        if (!StringUtils.isEmpty(admin.getUsername())){
            baseSql=baseSql+" and username like ?";
            params.add("%"+admin.getUsername()+"%");
        }
        if (!StringUtils.isEmpty(admin.getNickname())){
            baseSql=baseSql+" and nickname like ?";
            params.add("%"+admin.getNickname()+"%");
        }
        Map<String,Object>result=new HashMap<>();
        result.put("sql",baseSql);
        result.put("params",params);
        return result;

    }
//private Map<String, Object> getDynamicSql(Admin admin) {
//    String baseSsql = "select id,username as email,nickname,password as pwd from admin where 1 = 1 ";
//    List<String> params = new ArrayList<>();
//    if(!StringUtils.isEmpty(admin.getUsername())){
//        baseSsql = baseSsql + "and username like ?";
//        params.add("%" + admin.getUsername() + "%");
//    }
//    if(!StringUtils.isEmpty(admin.getNickname())){
//        baseSsql = baseSsql + "and nickname like ?";
//        params.add("%" + admin.getNickname() + "%");
//    }
//    Map<String, Object> result = new HashMap<>();
//    result.put("sql", baseSsql);
//    result.put("params", params);
//    return result;
//}
}
